//
// Settings.h
//


#pragma once

//#include "Source.h"

class CSettingsItem;

class CSettings  
{
// Construction
public:
	CSettings();
	virtual ~CSettings();
	
// Attributes
public:
	struct sGeneral
	{
		CString		Path;						//Installlation path for IndexMaker
		CString		UserPath;					//Path for user data. (May be the same as above for single user installs)
		BOOL		Debug;
		BOOL		DebugLog;
		BOOL		UpdateCheck;
		DWORD		DiskSpaceWarning;			//Value at which to warn the user about low disk space
		INT			GUIMode;
		BOOL		CloseMode;
		BOOL		TrayMinimise;
		BOOL		VerboseMode;
		BOOL		ShowTimestamp;
		BOOL		TestingMode;
		BOOL		SizeLists;
		BOOL		HashIntegrity;
		BOOL		RatesInBytes;
		DWORD		RatesUnit;
		BOOL		AlwaysOpenURLs;
		CString		UserAgent;
		CString		Language;
		BOOL		TopView;
	} General;

	struct sDebugParameter
	{
		BOOL	Default;
		BOOL	System;
		BOOL	Error;
		BOOL	Debug;
		BOOL	Temp;
		BOOL	Schedule;
		BOOL	RTSP;
		BOOL	Topology;
		BOOL	Tracker;
		BOOL	Media_Server;
		BOOL	Media_Client;
		BOOL	Partner;
		BOOL	DataPacket;
		BOOL	MediaServer;
		BOOL	MediaClient;
		BOOL	MiniServer;
		BOOL	RTSP_Msg;
		BOOL	Handshake;
		BOOL	Neighbour;
		BOOL	NetWork;
		BOOL	Channel;
		BOOL	Buffer;
		BOOL	Transfer;
	}DebugParameter;

	struct sInterface
	{
		DWORD		TipDelay;
		DWORD		TipAlpha;
		BOOL		TipSearch;
		BOOL		TipLibrary;
		BOOL		TipDownloads;
		BOOL		TipUploads;
		BOOL		TipNeighbours;
		BOOL		TipMedia;
		BOOL		LowResMode;
	} Interface;

	struct sMiniStreamingServer
	{
		CString		MiniHost;
		DWORD		MiniPort;
		DWORD		ClientsNumLimit;
	} MiniServer;


    struct sSourceServer
	{
		//modify by xianjian at 3.14
		//CString LocalDir;
        CList<CString>    LocalDir;

		CString		Host;
		DWORD		Port;
		DWORD		ClientsNumLimit;
	} SourceServer;


	struct sConnection
	{
		BOOL		AutoConnect;
		BOOL		Firewalled;
		CString		OutHost;
		CString		InHost;
		DWORD		InPort;
		BOOL		InBind;
		DWORD		InSpeed;
		DWORD		OutSpeed;
		BOOL		IgnoreLocalIP;				//Ingnore all 'local' (LAN) IPs
		BOOL		IgnoreOwnIP;				//Do not accept any ports on your external IP as a source
		DWORD		TimeoutConnect;
		DWORD		TimeoutHandshake;
		DWORD		TimeoutTraffic;
		DWORD		SendBuffer;
		BOOL		RequireForTransfers;		//Only upload/download to connected networks
		BOOL		AsyncIO;
		DWORD		LinkTimeout;
		DWORD		LinkPing;
		DWORD		MaxPartnerNum;
		DWORD       MaxActivePartnerNum;
		DWORD       MaxPassivePartnerNum;
		DWORD		MaxNeighbourNum;
	} Connection;

	struct sBandwidth
	{
		DWORD		Request;
		DWORD		HubIn;
		DWORD		HubOut;
		DWORD		LeafIn;
		DWORD		LeafOut;
		DWORD		PeerIn;
		DWORD		PeerOut;
		DWORD		UdpOut;
		DWORD		Downloads;
		DWORD		Uploads;
		DWORD		HubUploads;
	} Bandwidth;

	struct sCommunity
	{
		BOOL		ChatEnable;
		BOOL		Timestamp;
		BOOL		ServeProfile;
		BOOL		ServeFiles;
	} Community;


	struct sDiscovery
	{
		DWORD		AccessThrottle;
		DWORD		Lowpoint;
		DWORD		FailureLimit;
		DWORD		UpdatePeriod;
		DWORD		DefaultUpdate;
		DWORD		BootstrapCount;
		CString		G2DAddress;
		DWORD		G2DRetryAfter;
	} Discovery;
	
	struct sMediaMesh
	{
		BOOL		HubEnable;
		BOOL		HubForce;
		BOOL		LeafEnable;
		BOOL		LeafForce;
		DWORD		ConnectFactor;
		BOOL		DeflateHub2Hub;
		BOOL		DeflateLeaf2Hub;
		BOOL		DeflateHub2Leaf;
		DWORD		MaxResults;
		DWORD		MaxHits;
		DWORD		HitsPerPacket;
		DWORD		RouteCache;
		DWORD		HostCacheSize;
		DWORD		HostCacheExpire;
		DWORD		HostCacheView;
		DWORD		ConnectThrottle;

		BOOL		EnableToday;
		BOOL		EnableAlways;
		BOOL		Handshake04;
		BOOL		Handshake06;
		int			NumHubs;
		int			NumLeafs;
		int			NumPeers;
		DWORD		PacketBufferSize;
		DWORD		PacketBufferTime;
		DWORD		DefaultTTL;
		DWORD		SearchTTL;
		DWORD		TranslateTTL;
		DWORD		MaximumTTL;
		DWORD		MaximumPacket;
		DWORD		MaximumQuery;
		BOOL		StrictPackets;
		BOOL		EnableGGEP;
		BOOL		VendorMsg;
		DWORD		QueryThrottle;
		DWORD		RequeryDelay;
		DWORD		PingFlood;
		DWORD		PingRate;
		DWORD		PongCache;
		int			PongCount;
        DWORD       GossipTime;

		DWORD		UdpMTU;
		DWORD		UdpBuffers;
		DWORD		UdpInFrames;
		DWORD		UdpOutFrames;
		DWORD		UdpGlobalThrottle;
		DWORD		UdpOutExpire;
		DWORD		UdpOutResend;
		DWORD		UdpInExpire;

	} MediaMesh;

	struct sDownloads
	{
		CString		IncompletePath;				//Where incomplete downloads are stored
		CString		CompletePath;				//Where downloads are moved when they complete
		CString		TorrentPath;				//Where .torrent files are stored
		CString		CollectionPath;				//Where .collection and .co files are stored
		DWORD		BufferSize;
		DWORD		SparseThreshold;			//NTFS 'sparse files' are not used on files below this size. (0 = Disable)
		INT			MaxFiles;					//How many files download at once
		INT			MaxTransfers;				//How many total tranfers take place
		INT			MaxFileTransfers;			//How mnay transfers are allowed per file
		INT			MaxFileSearches;			//Number number of files over the download limit that prepare to start. (Search, etc)
		INT			MaxConnectingSources;		//The maximum number of sources that can be in the 'connecting' state. (Important for XPsp2)
		INT			MinSources;					//The minimum number of sources a download has before IndexMaker regards it as having a problem
		DWORD		ConnectThrottle;			//Delay between download attempts. (Very important for routers)
		INT			QueueLimit;					//Longest queue to wait in. (0 to disable. This should be >800 or 0 to get good performance from ed2k)
		DWORD		SearchPeriod;
		DWORD		StarveTimeout;
		DWORD		StarveGiveUp;				//How long (in hours) before IndexMaker will give up and try another download if it gets no data. (+ 0-9 h, depending on sources)
		DWORD		RetryDelay;
		DWORD		PushTimeout;
		BOOL		StaggardStart;
		BOOL		AllowBackwards;				//Permit download to run in reverse when appropriate
		DWORD		ChunkSize;
		DWORD		ChunkStrap;
		BOOL		Metadata;
		BOOL		VerifyFiles;
		BOOL		VerifyTiger;
		BOOL		VerifyED2K;
		BOOL		NeverDrop;					//Do not drop bad sources (may pollute source list with many dead sources)
		BOOL		RequestHash;
		BOOL		RequestHTTP11;
		BOOL		RequestURLENC;
		DWORD		SaveInterval;
		BOOL		FlushSD;
		BOOL		ShowSources;
		BOOL		ShowPercent;				//Display small green % complete bar on progress graphic
		BOOL		ShowGroups;
		BOOL		AutoExpand;
		BOOL		AutoClear;
		DWORD		ClearDelay;
		DWORD		FilterMask;
		BOOL		ShowMonitorURLs;
		BOOL		SortColumns;				//Allow user to sort downloads by clicking column headers
		BOOL		SortSources;				//Automatically sort sources (Status, protocol, queue)
	} Downloads;
	
	struct sUploads
	{
		int			MaxPerHost;
		DWORD		FreeBandwidthValue;
		DWORD		FreeBandwidthFactor;
		DWORD		ClampdownFactor;
		DWORD		ClampdownFloor;
		BOOL		ThrottleMode;
		DWORD		QueuePollMin;
		DWORD		QueuePollMax;
		DWORD		RotateChunkLimit;
		BOOL		SharePartials;
		BOOL		ShareTiger;
		BOOL		ShareHashset;
		BOOL		ShareMetadata;
		BOOL		SharePreviews;
		BOOL		DynamicPreviews;
		DWORD		PreviewQuality;
		DWORD		PreviewTransfers;
		BOOL		AllowBackwards;
		BOOL		HubUnshare;
		CString		BlockAgents;
		BOOL		AutoClear;
		DWORD		ClearDelay;
		DWORD		FilterMask;
	} Uploads;
	
	struct sRemote
	{
		BOOL		Enable;
		CString		Username;
		CString		Password;
	} Remote;
	
	struct sLive
	{
		DWORD		BandwidthScale;				//Monitor slider settings
		BOOL		LoadWindowState;
		BOOL		AutoClose;
		BOOL		FirstRun;
	} Live;

// Attributes : Item List
protected:
	CPtrList	m_pItems;
public:
	class Item
	{
	public:
		Item(LPCTSTR pszName, DWORD* pDword, DOUBLE* pFloat, CString* pString);
		void	Load();
		void	Save();
	public:
		CString		m_sName;
		DWORD*		m_pDword;
		DOUBLE*		m_pFloat;
		CString*	m_pString;
	};

// Operations
public:
	void	Load();
	void	Save(BOOL bShutdown = FALSE);
	Item*	GetSetting(LPCTSTR pszName) const;
	Item*	GetSetting(LPVOID pValue) const;
public:
	BOOL	LoadWindow(LPCTSTR pszName, CWnd* pWindow);
	void	SaveWindow(LPCTSTR pszName, CWnd* pWindow);
	BOOL	LoadList(LPCTSTR pszName, CListCtrl* pCtrl, int nSort = 0);
	void	SaveList(LPCTSTR pszName, CListCtrl* pCtrl);
	CString	SmartAgent(LPCTSTR pszAgent);
	CString	SmartVolume(QWORD nVolume, BOOL bInKB, BOOL bRateInBits = FALSE);
	QWORD	ParseVolume(LPCTSTR psz, BOOL bSpeedInBits);
	BOOL	CheckStartup();
	void	SetStartup(BOOL bStartup);

public:
   void		ModifyRealPlayerCfg();

protected:
	void	Setup();
	void	Add(LPCTSTR pszName, DWORD* pDword, DWORD nDefault);
	void	Add(LPCTSTR pszName, int* pDword, DWORD nDefault);
	void	Add(LPCTSTR pszName, DOUBLE* pFloat, DOUBLE nDefault);
	void	Add(LPCTSTR pszName, CString* pString, LPCTSTR pszDefault);
	void	SmartUpgrade();
};

extern CSettings Settings;

enum
{
	GUI_WINDOWED, GUI_TABBED, GUI_BASIC
};

#define MEDIAMESH_DEFAULT_PORT	6346
#define MINISERVER_DEFAULT_PORT 5555
#define MINISERVER_SUPPORT_CLIENTS_NUM  5


